<?php
	include_once('connexion.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by TEMPLATED
http://templated.co
Released for free under the Creative Commons Attribution License

Name       : Assembly 
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20140330

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kilbet - The first social betting platform</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="http://fonts.googleapis.com/css?family=Didact+Gothic" rel="stylesheet" />
<link href="styles/default.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/fonts.css" rel="stylesheet" type="text/css" media="all" />
<link rel="shortcut icon" type="image/x-icon" href="pictures/logo_kilbet.png" />

<!--[if IE 6]><link href="default_ie6.css" rel="stylesheet" type="text/css" /><![endif]-->

</head>

<body>
<?php include_once('header.php');?>
<?php
if(isset($userConnected))
{
	if(isset($_GET['userid']) && $userManager->exist((int) $_GET['userid']))
	{
		$userConsulted = $userManager->get((int) $_GET['userid']);
		//Test pour savoir si userConnected consulte son profil ou celui d'un autre => sera utilisé par la suite pour savoir si on est en mode édition ou non
		if($userConsulted == $userConnected) $myProfile=1;
		else $myProfile=0;
	}
	else header('Location: index.php');
}
else
{
	if(isset($_GET['from']) && $_GET['from']=="mail") include('login.php');
	else header('Location: index.php');
}
?>
<div id="big_content">
	<div id="profile_menu" class "container">
		<nav id="profile_inside_menu" class="inside_menu">
			<ul>
				<li class="title_inside_menu"><?php if($myProfile==1) echo 'Mon Profil'; else echo 'Profil de '.$userConsulted->login();?></li>
				<li <?php if($current=="/kilbet/profile.php" && (!isset($_GET['section']) || $_GET['section']=="profile")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?userid=<?php echo $userConsulted->id();?>&section=profile" alt="Profil">Profil</a></li>
				<?php if($myProfile==1) {?><li <?php if($current=="/kilbet/profile.php" && isset($_GET['section']) && ($_GET['section']=="messages" || $_GET['section']=="message_consultation")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?userid=<?php echo $userConsulted->id();?>&section=messages" alt="Messages">Messages</a></li><?php }?>
				<li  <?php if($current=="/kilbet/profile.php" && isset($_GET['section']) && $_GET['section']=="contest") echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?userid=<?php echo $userConsulted->id();?>&section=contest" alt="Concours"><?php if($myProfile==1) echo 'Mes concours'; else echo 'Ses concours';?></a></li>
				<div style="clear: both;"></div>
			</ul>
		</nav>
		<div style="clear: both;"></div>
	</div>
	<div id="content">
			<?php
				if(isset($_GET['userid']) && $userManager->exist((int) $_GET['userid']))
				{
					$userConsulted = $userManager->get((int) $_GET['userid']);
					//Test pour savoir si userConnected consulte son profil ou celui d'un autre => sera utilisé par la suite pour savoir si on est en mode édition ou non
					if($userConsulted == $userConnected) $myProfile=1;
					else $myProfile=0;
					?>
			
					<div id="profile_content" class="container">
					<?php
					//Si aucune section renseignée : affichage du profil par défaut	|| Si section = profile : affichage du profil
					if(!isset($_GET['section']) || $_GET['section'] == "profile")
					{
						?>
						<div id="section_personal_info">
							<h1 class="section_title">Informations personnelles</h1>
							<?php
							if(isset($updateAccountMessage) && $updateAccountMessage!="") echo '<p id="updateMessages">'.$updateAccountMessage.'</p>';
							
							//Chargement formulaire modification si user consulté = user connecté
							if($myProfile==1) echo '<form name="form_updateProfile" method="Post" action="profile_post.php?userid='.$userConsulted->id().'" id="form_modif_account" enctype="multipart/form-data">';
							else echo '<div id="form_modif_account">';
							
							//Affichage de la photo de profil
							?>
							<div id="profile_pic">
								<img src="
								<?php
								if(file_exists("pictures/profile_pictures/photo_".$userConsulted->id().".png")) echo "pictures/profile_pictures/photo_".$userConsulted->id().".png";
								else echo "pictures/profile_pictures/photo_undefined.png";
								?>
								" alt="Photo de profil" />
							</div>
							
							<div id="modif_profile">
								<?php
								
								//Login non-modifiable : on le restitue simplement dans tous les cas. Si modification : on envoie en champ hidden le login
								echo '<label>Login</label><span class="form_text">'.$userConsulted->login().'</span><br />';
								if($myProfile==1) echo '<input type="hidden" class="form_text" id="form_updateProfile_login" name="form_updateProfile_login" value="'.$userConsulted->login().'">';
								
								//Affichage ou modification civilité
								echo '<label>Civilité</label>';
								if($myProfile == 0) echo '<span class="form_text">'.$userConsulted->gender().'</span><br />';
								else
								{
									echo '<select id="form_updateProfile_gender" name="form_updateProfile_gender">
										<option value=""></option>';
										echo '<option value="M"';
											if($userConsulted->gender()=="M") echo 'selected="selected"';
										echo '>M.</option>';
										echo '<option value="Mme"';
											if($userConsulted->gender()=="Mme") echo 'selected="selected">Mme</option>';
										echo '>Mme</option>';
										echo '<option value="Melle"';
											if($userConsulted->gender()=="Melle") echo 'selected="selected">Melle</option>';
										echo '>Melle</option>';
									echo '</select><br />';
								}
								
								//Affichage ou modification prénom
								echo '<label>Prénom</label>';
								if($myProfile == 0) echo '<span class="form_text">'.$userConsulted->firstName().'</span><br />';
								else echo '<input type="text" class="form_text" id="form_updateProfile_firstName" name="form_updateProfile_firstName" value="'.$userConsulted->firstName().'" class="form_text"><br />';
								
								//Affichage ou modification nom
								echo '<label>Nom</label>';
								if($myProfile == 0) echo '<span class="form_text">'.$userConsulted->lastName().'</span><br />';
								else echo '<input type="text" class="form_text" id="form_updateProfile_lastName" name="form_updateProfile_lastName" value="'.$userConsulted->lastName().'" class="form_text"><br />';
								
								//Affichage ou modification mail
								echo '<label>Mail</label>';
								if($myProfile == 0) echo '<span class="form_text">'.$userConsulted->mail().'</span><br />';
								else echo '<input type="mail" class="form_text" id="form_updateProfile_mail" name="form_updateProfile_mail" value="'.$userConsulted->mail().'" class="form_text" onblur="checkFieldNotEmpty(this);"><br />';
								
								//Affichage ou modification téléphone
								echo '<label>Téléphone</label>';
								if($myProfile == 0) echo '<span class="form_text">'.$userConsulted->tel().'</span><br />';
								else echo '<input type="mail" class="form_text" id="form_updateProfile_tel" name="form_updateProfile_tel" value="'.$userConsulted->tel().'" class="form_text"><br />';
								
								
								$_SESSION['updateAccountMessage']="";
								$_SESSION['updateFirstName']="";
								$_SESSION['updateLastName']="";
								$_SESSION['updateEmail']="";
								$_SESSION['updateTel']="";
								$_SESSION['updateGender']="";
								
								?>
							</div>
							
							<div style="clear: both;"></div>
							
							<?php
							
							//Modification mot de passe si user consulté = user connecté
							if($myProfile == 1)
							{
								?>
								<br />
								<h2>Modifier mon mot de passe</h2>
								<?php
								echo '<label for="form_updateProfile_pass">Nouveau mot de passe</label><input type="password" class="form_text" id="form_updateProfile_pass" name="form_updateProfile_pass"><br />';
								echo '<input type="hidden" class="form_text" id="form_updateProfile_passH" name="form_updateProfile_passH" value="'.$userConsulted->password().'">';
								echo '<label for="form_updateProfile_pass2">Confirmer nouveau mot de passe</label><input type="password" class="form_text" id="form_updateProfile_pass2" name="form_updateProfile_pass2"><br />';
							}
							
							//Modification photo de profil si user consulté = user connecté
							if($myProfile == 1)
							{
								?>
								<br />
								<h2>Modifier ma photo de profil</h2>
								<?php
								echo '<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />';
								echo '<label for="form_updateProfile_picture"></label><input type="file" class="form_file" id="form_updateProfile_picture" name="form_updateProfile_picture"><br />';
							}
							
							//Validation formulaire si user consulté = user connecté
							if($myProfile == 1) echo '<br /><br /><input type="submit" id="form_updateProfile_submit" name="form_updateProfile_submit" value="Modifier" class="form_submit">';
							
							//Fermeture balise
							if($myProfile == 1) echo '</form>';
							else echo '</div>';
							?>
							<div style="clear: both;"></div>
						</div>
						<?php
					}
					
					elseif($_GET['section'] == "messages")
					{
						?>
						<div id="section_messages">
						<?php
						//Si user connecté = user consulté : on affiche les messages
						if($myProfile==1)
						{
							?>
							<div id="section_messages_inbox">
							<table id="board" summary="mycontests_board" class="tab">
							  <thead>
								<tr>
								<th id="sender_col" scope="col">Emetteur</th>
								  <th id="subject_col" scope="col">Sujet</th>
								  <th id="date_col"scope="col">Date</th>
								</tr>
							  </thead>
							  <tfoot>
								<tr>
								  <td colspan="5">Messages reçus</td>
								</tr>
							  </tfoot>
							  <tbody>
								<?php
								$messages = $userManager->getListMessagesReceivedInbox($userConnected);
								if(count($messages) == 0)
								{
									?>
									<tr>
										<td colspan="5">Aucun message reçu</td>
									</tr>
									<?php
								}
								else
								{
									foreach($messages as $message)
									{
										?>
										<tr>
											<td>
											<?php
											if($message->messageFrom() != "") echo $userManager->get($message->messageFrom())->login();
											else echo "No-reply Kilbet";
											?>
											</td>
											<td><a href="?userid=<?php echo $userConsulted->id();?>&section=message_consultation&messageid=<?php echo $message->id();?>" alt="Consulter ce mail"><?php echo $message->subject();?></a></td>
											<td><?php echo $message->messageDate();?></td>
										</tr>
										<?php
									}
								}
								?>
							</table>
							</div>
							<div id="section_messages_sent">
							<table id="board" summary="mycontests_board" class="tab">
							  <thead>
								<tr>
								<th id="mailto_col"scope="col">Destinataire</th>
								  <th id="subject_col"scope="col">Sujet</th>
								  <th id="date_col"scope="col">Date</th>
								</tr>
							  </thead>
							  <tfoot>
								<tr>
								  <td colspan="5">Messages envoyés</td>
								</tr>
							  </tfoot>
							  <tbody>
								<?php
								$messages = $userManager->getListMessagesSentInbox($userConnected);
								if(count($messages) == 0)
								{
									?>
									<tr>
										<td colspan="5">Aucun message envoyé</td>
									</tr>
									<?php
								}
								else
								{
									foreach($messages as $message)
									{
										?>
										<tr>
											<td>
											<?php
											//Affichage de la liste des destinataires du message
											if($message->messageTo() != "" && $message->messageTo() != "0")
											{
												$messageTo=explode(';', $message->messageTo());
												$i = 0;
												foreach($messageTo as $to)
												{
													$to = (int) substr($to, 1);
													if($userManager->exist($to))
													{
														if($i > 0) echo ' - ';
														{
															echo $userManager->get($to)->login();
															$i++;
														}
													}
												}
											}
											//Si message envoyé à une personne pas encore enregistrée chez Kilbet : affichage de l'adresse mail
											else echo $message->mailTo();
											?>
											</td>
											<td><a href="?userid=<?php echo $userConsulted->id();?>&section=message_consultation&messageid=<?php echo $message->id();?>" alt="Consulter ce mail"><?php echo $message->subject();?></a></td>
											<td><?php echo $message->messageDate();?></td>
										</tr>
										<?php
									}
								}
								?>
							</table>
							</div>
							</div>
							<?php
						}
					}
					
					elseif($_GET['section'] == "message_consultation")
					{
						//Section dans laquelle on affiche la page de consultation d'un message précis
						include_once('message.php');
					}
					
					elseif($_GET['section'] == "contest")
					{
						include_once('mycontests.php');
					}
				}
				//Si pas de userid renseigné dans l'URL ou s'il n'exite pas : on redirige vers la page index.php
				else header('Location: index.php');
			?>
		</div>
	</div>
</div>
<?php include_once('footer.php');?>
	
</body>
</html>